<script setup>
import { ElMessage } from 'element-plus';
import { ref,getCurrentInstance } from 'vue';
const props = defineProps({
  pkg: Object
});
const { $t } = getCurrentInstance().proxy;
const emit = defineEmits(['add-to-cart']);
const processing = ref(false)
const handleAddToCart = () => {
  if (processing.value || props.pkg.added) return
  processing.value = true
  setTimeout(() => {
    emit('add-to-cart', props.pkg);
    ElMessage.success($t("addSuccess"));
    processing.value = false;
  }, 1000);
}
</script>

<template>
  <div class="grid-item">
    <div class="item-badge" v-if="pkg.id === 1">{{ $t("badge1") }}</div>
    <div class="item-badge new" v-if="pkg.id === 4">{{ $t("badge2") }}</div>
    <div class="item-badge discount" v-if="[3,7].includes(pkg.id)">{{ $t("badge3") }}</div>

    <div class="item-content">
      <h3 class="item-title">{{ pkg.name }}</h3>
      <p class="item-description">{{ pkg.description }}</p>
      <div class="item-price">¥ {{ pkg.price }}</div>
      <ul class="item-features">
        <li v-if="pkg.id === 1">
          <i class="fas fa-check-circle"></i> {{ $t("feature1_1") }}
        </li>
        <li v-if="pkg.id === 2">
          <i class="fas fa-check-circle"></i> {{ $t("feature2_1") }}
        </li>
        <li v-if="pkg.id === 2">
          <i class="fas fa-check-circle"></i> {{ $t("feature2_2") }}
        </li>
        <li v-if="pkg.id === 3">
          <i class="fas fa-check-circle"></i> {{ $t("feature3_1") }}
        </li>
        <li v-if="pkg.id === 4">
          <i class="fas fa-check-circle"></i> {{ $t("feature4_1") }}
        </li>
        <li v-if="pkg.id === 5">
          <i class="fas fa-check-circle"></i> {{ $t("feature5_1") }}
        </li>
        <li v-if="pkg.id === 6">
          <i class="fas fa-check-circle"></i> {{ $t("feature6_1") }}
        </li>
        <li v-if="pkg.id === 6">
          <i class="fas fa-check-circle"></i> {{ $t("feature6_2") }}
        </li>
        <li v-if="pkg.id === 7">
          <i class="fas fa-check-circle"></i> {{ $t("feature7_1") }}
        </li>
        <li v-if="pkg.id === 8">
          <i class="fas fa-check-circle"></i> {{ $t("feature8_1") }}
        </li>
        <li v-if="pkg.id === 8">
          <i class="fas fa-check-circle"></i> {{ $t("feature8_2") }}
        </li>
      </ul>
    </div>

    <button class="buy-button" :class="{ added: pkg.added , owned: pkg.owned}" @click="handleAddToCart"
      :disabled="pkg.owned || pkg.added || processing">
      <span v-if="pkg.owned"><i class="fas fa-check"></i> {{ $t("owned") }}</span>
      <span v-else-if="!processing && !pkg.added">{{ $t("addToCart") }}</span>
      <span v-else-if="processing"><i class="fas fa-spinner fa-spin"></i> {{ $t("processing") }}</span>
      <span v-else><i class="fas fa-cart-plus"></i> {{ $t("added") }}</span>
    </button>
  </div>
</template>

<style scoped>
.grid-item {
  background: var(--card-bg);
  border-radius: 15px;
  padding: 25px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.grid-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.12);
}

.item-badge {
  position: absolute;
  top: 15px;
  right: 15px;
  background: #ff6b6b;
  color: white;
  padding: 5px 15px;
  border-radius: 20px;
  font-size: 0.9rem;
  font-weight: 600;
  z-index: 1;
}

.item-badge.new {
  background: #2ecc71;
}

.item-badge.discount {
  background: #4facfe;
}

.item-content {
  flex-grow: 1;
}

.item-title {
  font-size: 1.4rem;
  margin-bottom: 12px;
  color: var(--cart-text);
  font-weight: 700;
}

.item-description {
  font-size: 1rem;
  color: #7f8c8d;
  margin-bottom: 15px;
  line-height: 1.5;
}

.item-price {
  font-size: 1.8rem;
  font-weight: bold;
  margin: 15px 0;
  color: #4facfe;
}

.item-features {
  list-style-type: none;
  margin: 15px 0;
  padding: 0;
}

.item-features li {
  display: flex;
  align-items: center;
  font-size: 0.95rem;
  margin-bottom: 8px;
  color: var(--cart-desc);
}

.item-features li i {
  color: #4facfe;
  margin-right: 8px;
  font-size: 0.9rem;
}

.buy-button {
  background: linear-gradient(45deg, #4facfe, #00f2fe);
  color: white;
  border: none;
  padding: 12px;
  border-radius: 10px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 15px;
  transition: all 0.3s ease;
  width: 100%;
  font-size: 1rem;
  opacity: var(--page-opacity);
}

.buy-button:hover:not(:disabled) {
  background: linear-gradient(45deg, #3a9df8, #00d9e6);
  transform: scale(1.02);
}

.buy-button:disabled {
  background: #2ecc71;
  cursor: not-allowed;
  opacity: var(--page-opacity);
}

.buy-button.added {
  background: #2ecc71;
  opacity: var(--page-opacity);
}

.buy-button.owned {
  background: #95a5a6 !important;
  opacity: var(--page-opacity);
}
</style>